package erupt_simultaneously;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Auther Jun jie Yi
 * @Date 19:36 2021/8/5
 */
public class FutureTaskDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //FutureTask 能够接收 Callable 类型的参数，用来处理有返回结果的情况
        // 创建任务对象
        FutureTask<Integer> task3 = new FutureTask<>(() -> {
            System.out.println("hello");
            return 100;
        });

        // 参数1 是任务对象; 参数2 是线程名字，推荐
        Thread thread = new Thread(task3, "t3");
        thread.start();

        Thread.currentThread().getThreadGroup().list();

        // 主线程阻塞，同步等待 task 执行完毕的结果
        Integer result = task3.get();
        System.out.println("结果是:{}" + result);

    }
}